package com.services.impl;

import com.dao.IFlightDao;
import com.dao.impl.FlightDaoImpl;
import com.model.FlightModel;
import com.model.PageBeanMode;
import com.services.IFlightServices;

import java.util.List;
import java.util.Map;

/**
 * @Program: exam
 * @ClassName: FlightServicesImpl
 * @Description:
 * @Author: li
 * @Create: 2019-07-24 13:52
 */
public class FlightServicesImpl implements IFlightServices {
    IFlightDao dao = new FlightDaoImpl();

    //将航班进行排序
    //1:按租金升序，0:按航班排序,-1:按租金降序
    @Override
    public List<FlightModel> sortFlights(int status) {
        if(status == 1){
            return dao.queryAllFlightByRentAsc();
        }else if (status == 0){
            return dao.queryAllFlight();
        }else {
            return dao.queryAllFlightByRentDesc();
        }
    }

    @Override
    public List<FlightModel> fuzzyQueryByFlight(String cname) {
        StringBuilder sql = new StringBuilder("SELECT * FROM flightport WHERE 1=1");
        
        // 如果有搜索条件，添加模糊查询
        if (cname != null && !cname.trim().isEmpty()) {
            sql.append(" AND cname LIKE '%").append(cname).append("%'");
        }
        
        // 只查询上架的航班
        //sql.append(" AND status = 1");
        
        System.out.println("执行的SQL: " + sql.toString()); // 调试日志
        return dao.fuzzyQueryByFlight(sql.toString());
    }

    @Override
    public FlightModel queryFlightById(int id) {
        return dao.queryFlightById(id);
    }

    @Override
    public PageBeanMode<FlightModel> pageFlight(String _pageStart, String _pageSize, Map<String, String[]> map) {
        int pageStart = Integer.parseInt(_pageStart);
        int pageSize = Integer.parseInt(_pageSize);

        List<FlightModel> flightModels = dao.pageFlight((pageStart - 1) * pageSize, pageSize, map);
        int totalData = dao.totalData(map);
        return new PageBeanMode<FlightModel>(pageStart, pageSize, totalData, flightModels);
    }

    @Override
    public void changFlightDown(int fid) {
        dao.changFlightDown(fid);
    }

    @Override
    public void addFlight(FlightModel flightModel) {
        dao.addFlight(flightModel);
    }

    @Override
    public void updateFlight(FlightModel flightModel) {
        dao.updateFlight(flightModel);
    }

    @Override
    public void changeFlightStatus(int id, int status) {
        dao.changeFlightStatus(id,status);
    }

    @Override
    public List<FlightModel> queryAllFlight() {
        return dao.queryAllFlight();
    }

}
